﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace pigs
{
    public class HighScores
    {
        public HighScoreEntry[] ontScore;
        public HighScoreEntry[] skScore;
        public HighScoreEntry[] bcScore;
        public HighScoreEntry[] surScore;

        public HighScores()
        {
            ontScore = new HighScoreEntry[10];
            skScore = new HighScoreEntry[10];
            bcScore = new HighScoreEntry[10];
            surScore = new HighScoreEntry[10];

            StreamReader reader;
            Boolean fileExists = true;

            try
            {
                reader = new StreamReader("highscores.txt", false);
                reader.Close();
            }
            catch
            {
                fileExists = false;
            }

            if (fileExists)
            {
                reader = new StreamReader("highscores.txt", false);
                try
                {
                    String name;
                    String scoreString;

                    reader.ReadLine();
                    for (int i = 0; i < 10; i++)
                    {
                        name = toString(reader.ReadLine());
                        scoreString = toString(reader.ReadLine());
                        int score = stringToNum(scoreString);
                        ontScore[i] = new HighScoreEntry(name, score);
                    }

                    reader.ReadLine();
                    for (int i = 0; i < 10; i++)
                    {
                        name = toString(reader.ReadLine());
                        scoreString = toString(reader.ReadLine());
                        int score = stringToNum(scoreString);
                        skScore[i] = new HighScoreEntry(name, score);
                    }

                    reader.ReadLine();
                    for (int i = 0; i < 10; i++)
                    {
                        name = toString(reader.ReadLine());
                        scoreString = toString(reader.ReadLine());
                        int score = stringToNum(scoreString);
                        bcScore[i] = new HighScoreEntry(name, score);
                    }

                    reader.ReadLine();
                    for (int i = 0; i < 10; i++)
                    {
                        name = toString(reader.ReadLine());
                        scoreString = toString(reader.ReadLine());
                        int score = stringToNum(scoreString);
                        surScore[i] = new HighScoreEntry(name, score);
                    }
                    reader.Close();
                }
                catch
                {
                    reader.Close();
                    makeDefaultScores();
                }
            }
            else
            {
                makeDefaultScores();
            }
        }

        public void makeDefaultScores()
        {
            for (int i = 9; i >= 0; i--)
            {
                ontScore[i] = new HighScoreEntry("Sam", -1 * (i - 10) * 1000);
                skScore[i] = new HighScoreEntry("Sam", -1 * (i - 10) * 1000);
                bcScore[i] = new HighScoreEntry("Sam", -1 * (i - 10) * 1000);
                surScore[i] = new HighScoreEntry("Sam", -1 * (i - 10) * 1000);
            }
            this.saveScores();
        }

        public int stringToNum(String scoreString)
        {
            int score = 0;
            for (int i = 0; i < scoreString.Length; i++)
            {
                if (scoreString.ElementAt(i).ToString().Equals("1")) score = score * 10 + 1;
                if (scoreString.ElementAt(i).ToString().Equals("2")) score = score * 10 + 2;
                if (scoreString.ElementAt(i).ToString().Equals("3")) score = score * 10 + 3;
                if (scoreString.ElementAt(i).ToString().Equals("4")) score = score * 10 + 4;
                if (scoreString.ElementAt(i).ToString().Equals("5")) score = score * 10 + 5;
                if (scoreString.ElementAt(i).ToString().Equals("6")) score = score * 10 + 6;
                if (scoreString.ElementAt(i).ToString().Equals("7")) score = score * 10 + 7;
                if (scoreString.ElementAt(i).ToString().Equals("8")) score = score * 10 + 8;
                if (scoreString.ElementAt(i).ToString().Equals("9")) score = score * 10 + 9;
                if (scoreString.ElementAt(i).ToString().Equals("0")) score = score * 10 + 0;
            }
            return score;
        }

        public String toHex(String input)
        {
            string result = "";
            char[] values = input.ToCharArray();
            foreach (char letter in values)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(letter);

                // Convert the decimal value to a hexadecimal value in string form.
                string hexOutput = String.Format("{0:X}", value);
                result = result + hexOutput + " ";
            }
            return result;
        }

        public string toString(String input)
        {
            String result = "";

            input = input.Substring(0, input.Length - 1);
            string[] inputSplit = input.Split(' ');
            foreach (String hex in inputSplit)
            {
                // Convert the number expressed in base-16 to an integer.
                int value = Convert.ToInt32(hex, 16);

                // Get the character corresponding to the integral value.
                result = result + Char.ConvertFromUtf32(value);
            }
            return result;
        }

        public void saveScores()
        {
            StreamWriter writer = new StreamWriter("highscores.txt", false);

            writer.WriteLine("ont");
            for (int i = 0; i < 10; i++)
            {
                writer.WriteLine(toHex(ontScore[i].name));
                writer.WriteLine(toHex(ontScore[i].score.ToString()));
            }

            writer.WriteLine("sk");
            for (int i = 0; i < 10; i++)
            {
                writer.WriteLine(toHex(skScore[i].name));
                writer.WriteLine(toHex(skScore[i].score.ToString()));
            }

            writer.WriteLine("bc");
            for (int i = 0; i < 10; i++)
            {
                writer.WriteLine(toHex(bcScore[i].name));
                writer.WriteLine(toHex(bcScore[i].score.ToString()));
            }
            
            writer.WriteLine("sur");
            for (int i = 0; i < 10; i++)
            {
                writer.WriteLine(toHex(surScore[i].name));
                writer.WriteLine(toHex(surScore[i].score.ToString()));
            }
            
            writer.Close();
        }

        public bool newHighScore(string levelName, int score)
        {
            if (levelName.Equals("ont") && ontScore[9].score < score)
            {
                return true;
            }
            if (levelName.Equals("sk") && skScore[9].score < score)
            {
                return true;
            }
            if (levelName.Equals("bc") && bcScore[9].score < score)
            {
                return true;
            }
            if (levelName.Equals("sur") && surScore[9].score < score)
            {
                return true;
            }
            return false;
        }

        public void sortHighScores(HighScoreEntry[] scoreList)
        {
            for (int i = scoreList.Length - 1; i > 0; i--)
            {
                if (scoreList[i].score > scoreList[i - 1].score)
                {
                    HighScoreEntry temp = scoreList[i - 1];
                    scoreList[i - 1] = scoreList[i];
                    scoreList[i] = temp;
                }
            }
        }

        public void updateScores(String levelName, String name, int score)
        {
            if (levelName.Equals("ont") && ontScore[9].score < score)
            {
                ontScore.Last().name = name;
                ontScore.Last().score = score;
                sortHighScores(ontScore);
            }
            if (levelName.Equals("sk") && skScore[9].score < score)
            {
                skScore.Last().name = name;
                skScore.Last().score = score;
                sortHighScores(skScore);
            }
            if (levelName.Equals("bc") && bcScore[9].score < score)
            {
                bcScore.Last().name = name;
                bcScore.Last().score = score;
                sortHighScores(bcScore);
            }
            if (levelName.Equals("sur") && surScore[9].score < score)
            {
                surScore.Last().name = name;
                surScore.Last().score = score;
                sortHighScores(surScore);
            }
            saveScores();
        }
    }
}
